﻿namespace Home.Security.Authentication.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    using Home.Data;
    using Home.Services;

    internal class AuthenticationEntity : Entity
    {
        private IDictionary<string, string> authenticationProperties;
        private IDictionary<string, string> authorizationProperties;
        private IDictionary<string, string> accessTokenProperties;
        private IDictionary<string, string> refreshTokenProperties;
        private IDictionary<string, string> snsProperties;

        [DataMember]
        public Guid TrackId { get; set; }

        [DataMember]
        public AuthenticationType AuthenticationType { get; set; }

        // authentication

        [DataMember]
        public string AuthenticationUri { get; set; }

        [DataMember]
        public DateTimeOffset AuthenticationDateTime { get; set; }

        public IDictionary<string, string> AuthenticationProperties
        {
            get
            {
                if (this.authenticationProperties == null)
                {
                    this.authenticationProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                }

                return this.authenticationProperties;
            }
            set { this.authenticationProperties = value; }
        }

        [DataMember]
        public string AuthenticationPropertiesString
        {
            get { return this.authenticationProperties.ToRepositoryProperties(); }
            set { this.authenticationProperties = value.ToEntityProperties(); }
        }

        // authorization

        [DataMember]
        public string AuthorizationCallbackUri { get; set; }

        [DataMember]
        public string AuthorizationCode { get; set; }

        [DataMember]
        public AuthenticationStatus AuthorizationStatus { get; set; }

        public IDictionary<string, string> AuthorizationProperties
        {
            get
            {
                if (this.authorizationProperties == null)
                {
                    this.authorizationProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                }

                return this.authorizationProperties;
            }
            set { this.authorizationProperties = value; }
        }

        [DataMember]
        public string AuthorizationPropertiesString
        {
            get { return this.authorizationProperties.ToRepositoryProperties(); }
            set { this.authorizationProperties = value.ToEntityProperties(); }
        }

        // access token

        [DataMember]
        public string AccessTokenUri { get; set; }

        [DataMember]
        public string AccessToken { get; set; }

        [DataMember]
        public string RefreshToken { get; set; }

        public IDictionary<string, string> AccessTokenProperties
        {
            get
            {
                if (this.accessTokenProperties == null)
                {
                    this.accessTokenProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                }

                return this.accessTokenProperties;
            }
            set { this.accessTokenProperties = value; }
        }

        [DataMember]
        public string AccessTokenPropertiesString
        {
            get { return this.accessTokenProperties.ToRepositoryProperties(); }
            set { this.accessTokenProperties = value.ToEntityProperties(); }
        }

        [DataMember]
        public string AccessTokenResponseHeader { get; set; }

        [DataMember]
        public string AccessTokenResponseBody { get; set; }

        // refresh token

        [DataMember]
        public string RefreshTokenUri { get; set; }

        public IDictionary<string, string> RefreshTokenProperties
        {
            get
            {
                if (this.refreshTokenProperties == null)
                {
                    this.refreshTokenProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                }

                return this.refreshTokenProperties;
            }
            set { this.refreshTokenProperties = value; }
        }

        [DataMember]
        public string RefreshTokenPropertiesString
        {
            get { return this.refreshTokenProperties.ToRepositoryProperties(); }
            set { this.refreshTokenProperties = value.ToEntityProperties(); }
        }

        [DataMember]
        public string RefreshTokenResponseHeader { get; set; }

        [DataMember]
        public string RefreshTokenResponseBody { get; set; }

        // SNS

        [DataMember]
        public string SnsUri { get; set; }

        public IDictionary<string, string> SnsProperties
        {
            get
            {
                if (this.snsProperties == null)
                {
                    this.snsProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                }

                return this.snsProperties;
            }
            set { this.snsProperties = value; }
        }

        [DataMember]
        public string SnsPropertiesString
        {
            get { return this.snsProperties.ToRepositoryProperties(); }
            set { this.snsProperties = value.ToEntityProperties(); }
        }

        [DataMember]
        public string SnsResponseHeader { get; set; }

        [DataMember]
        public string SnsResponseBody { get; set; }
    }
}
